<?php
require_once __DIR__ . '/Load.php';
use Config\Cfg;
use Controller\LobbyController;
use Controller\UserController;
use Controller\WebController;
use Extension\Tools;
use Handler\LobbyHandler;
use Handler\PushHandler;
use Handler\SFSHandler;
use Handler\UserHandler;
use Extension\Tool_Des;
class Main {
	/**
	 * 单例
	 *
	 * @var Main
	 */
	private static $m_Instance;
	/**
	 *
	 * @var array
	 */
	private $m_Controllers;
	/**
	 *
	 * @var Tool_Des
	 */
	private $m_ToolDes;
	/**
	 * 构造函数
	 */
	private function __construct() {
		gc_enable ();
		$this->m_ToolDes = new Tool_Des ( Cfg::DES_KEY, Cfg::DES_IV );
		$this->mb_Init ();
	}
	/**
	 * 复制函数
	 */
	private function __clone() {
	}
	/**
	 * 获取单例
	 *
	 * @return Main
	 */
	public static function mb_GetInstance() {
		if (! (self::$m_Instance instanceof self)) {
			self::$m_Instance = new self ();
		}
		return self::$m_Instance;
	}
	/**
	 * 初始化配置
	 */
	public function mb_Init() {
		$this->m_Controllers = array (
				'Lobby' => new LobbyController (),
				'User' => new UserController (),
				'Web' => new WebController () 
		);
		LobbyHandler::mb_GetInstance ();
		PushHandler::mb_GetInstance ();
		UserHandler::mb_GetInstance ();
		SFSHandler::mb_GetInstance ();
		
		// $reqSFS = new Req_sfslobby();
		// echo "-------------------账号注册-------------------\n";
		// $reqData = $reqSFS->AccountRegist('max003', 'max003', '000003');
		// print_r($reqData);
		// echo "\n-------------------账号登录-------------------\n";
		// $reqData = $reqSFS->AccountLogin('max001', '000001');
		// print_r($reqData);
		// echo "\n-------------------查询账号信息-------------------\n";
		// $reqData = $reqSFS->QueryAccountData('max001');
		// print_r($reqData);
		// echo "\n-------------------保存账号信息-------------------\n";
		// $reqData = $reqSFS->SaveAccountData('max001', 10000);
		// print_r($reqData);
		// echo "\n-------------------查询账号状态-------------------\n";
		// $reqData = $reqSFS->QueryAccountStatus('max001');
		// print_r($reqData);
		
		// $desObj = new STD3Des(Cfg::MSG_MD5, '87654321');
		// $desStr = $desObj->encrypt("Hello My Des!!!!");
		
		// // $desStr = Tool_Des::mb_Encode("Hello My Des!!!!", Cfg::MSG_MD5);
		// echo "加密： $desStr \n";
		// $desStr = $desObj->decrypt($desStr);
		// echo "解密： $desStr \n";
	}
	/**
	 * 重新获取配置
	 */
	public function mb_Reload() {
	}
	/**
	 * 客户端连接
	 *
	 * @param string $clientId        	
	 */
	function mb_OnConnect($clientId) {
	}
	/**
	 * 客户端断开连接
	 *
	 * @param string $clientId        	
	 */
	function mb_OnClose($clientId) {
		UserHandler::mb_GetInstance ()->mb_OnClose ( $clientId );
	}
	/**
	 * 客户端发来消息
	 *
	 * @param string $clientId        	
	 * @param string $message        	
	 */
	function mb_OnTextMessage($clientId, $message) {
		try {
			$desMsg = json_decode ( $message );
			if (json_last_error () != JSON_ERROR_NONE) {
				return;
			}
			if (md5 ( $desMsg->msg . Cfg::MSG_MD5 ) == $desMsg->sign) {
				
				$jsonMsg = json_decode ( $this->m_ToolDes->decrypt ( $desMsg->msg ) );
				
				$zone = $jsonMsg->m_Zone;
				$method = $jsonMsg->m_Method;
				$data = $jsonMsg->m_Data;

				call_user_func_array ( array (
						$this->m_Controllers [$zone],
						$method
				), array (
						$clientId,
						json_decode ( $data )
				) );
			}
		} catch ( Exception $e ) {
			Tools::mb_LogResult ( $e );
		}
	}
	/**
	 * 客户端发来消息
	 *
	 * @param string $clientId        	
	 * @param array $message        	
	 */
	function mb_OnHttpMessage($clientId, $message) {
		if (isset ( $message ['post'] ['data'] )) {
			// Post方法入口
			$jsonMsg = json_decode ( $message ['post'] ['data'] );
		} else if (isset ( $message ['get'] ['data'] )) {
			// Get方法入口
			$jsonMsg = json_decode ( $message ['get'] ['data'] );
		} else {
			return;
		}
		
		if (json_last_error () != JSON_ERROR_NONE) {
			return;
		}
		
		$zone = $jsonMsg->m_Zone;
		$method = $jsonMsg->m_Method;
		$data = $jsonMsg->m_Data;
		$sign = $jsonMsg->m_Sign;
		
		if ($sign == md5 ( $zone . $method . $data . Cfg::MSG_MD5 )) {
			
			call_user_func_array ( array (
					$this->m_Controllers [$zone],
					$method 
			), array (
					$clientId,
					json_decode ( $data ) 
			) );
		} else {
			echo "errer request! \n";
		}
	}
	/**
	 * 客户端发来消息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	function mb_OnWebMessage($clientId, $message) {
		if (count ( $message ['get'] ) == 0)
			return;
		
		$zone = $message ['get'] ['zone'];
		$method = $message ['get'] ['method'];
		
		call_user_func_array ( array (
				$this->m_Controllers [$zone],
				$method 
		), array (
				$clientId,
				$message ['get'] 
		) );
	}
}